import 'package:after_sale_plugin/package_data_center.dart';
import 'package:after_sale_plugin/package_event.dart';
import 'package:azlistview/azlistview.dart';
import 'package:get/get.dart';
import 'package:lpinyin/lpinyin.dart';
import 'package:lx_widgets_library/custom_controller/scroll_opacity_controller.dart';

import 'area_model.dart';

class AreaListController extends LxBaseScrollOpacityController {
  RxList<AreaModel> cityList = <AreaModel>[].obs;

  @override
  void onInit() {
    super.onInit();

    if (PackageDataCenter.cityCodeList != null) {
      cityList.value = PackageDataCenter.cityCodeList!;
      _handleList(cityList);
    } else {
      PackageEvent.getCountryCode((p0) {
        cityList.value = p0;
        _handleList(p0);
      });
    }
  }

  void _handleList(List<AreaModel> list) {
    if (list.isEmpty) return;
    for (int i = 0, length = list.length; i < length; i++) {
      /// 获取拼音首字母
      String pinyin = PinyinHelper.getPinyinE(list[i].name!);
      // String pinyin = PinyinHelper.convertToPinyinStringWithoutException(list[i].name);
      String tag = pinyin.substring(0, 1).toUpperCase();
      list[i].namePinyin = pinyin;
      if (RegExp("[A-Z]").hasMatch(tag)) {
        list[i].tagIndex = tag;
      } else {
        list[i].tagIndex = "#";
      }
    }
    // A-Z sort.
    SuspensionUtil.sortListBySuspensionTag(cityList);

    // show sus tag.
    SuspensionUtil.setShowSuspensionStatus(cityList);
  }
}
